{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3fef4e91",
   "metadata": {},
   "source": [
    "# week09作业：\n",
    "1. 请尝试使用和学习字典的方法（必须完成）\n",
    "2. 给一组数据，利用字典查询的方式尝试查找指定的数据（尝试完成）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf1ed92e",
   "metadata": {},
   "source": [
    "## 字典（无序）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "5fea377d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'coco', 'age': 19, 'height': 1.58, 'weight': 50}\n"
     ]
    }
   ],
   "source": [
    "coco = {\"name\":\"coco\",\n",
    "        \"age\": 19,\n",
    "        \"height\":1.58,\n",
    "        \"weight\":50}\n",
    "print(coco)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e196c18f",
   "metadata": {},
   "source": [
    "### 取值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "55b54b78",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "coco\n",
      "19\n"
     ]
    }
   ],
   "source": [
    "print(coco[\"name\"])\n",
    "print(coco[\"age\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61e0e8c0",
   "metadata": {},
   "source": [
    "### 增加/修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "2883ed85",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18\n"
     ]
    }
   ],
   "source": [
    "coco[\"age\"]=18\n",
    "print(coco[\"age\"])\n",
    "# 如果key存在会修改已存在的key对应的value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "5904cdf7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'coco', 'age': 18, 'height': 1.58, 'weight': 50, 'gender': 'female'}\n"
     ]
    }
   ],
   "source": [
    "coco[\"gender\"]=\"female\"\n",
    "print(coco)\n",
    "# 如果key不存在，会直接增加"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c8d6383",
   "metadata": {},
   "source": [
    "### 删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "c380e57e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'age': 18, 'height': 1.58, 'weight': 50, 'gender': 'female'}\n"
     ]
    }
   ],
   "source": [
    "coco.pop(\"name\")\n",
    "print(coco)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26b68b7e",
   "metadata": {},
   "source": [
    "### 统计数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "c2c4368e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n"
     ]
    }
   ],
   "source": [
    "print(len(coco))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "beeb793a",
   "metadata": {},
   "source": [
    "### 合并"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "51704f39",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'cindy', 'age': 16, 'height': 1.68, 'weight': 48}\n"
     ]
    }
   ],
   "source": [
    "cindy = {\"name\":\"cindy\",\n",
    "         \"age\":16,\n",
    "         \"height\":1.68,\n",
    "         \"weight\":48}\n",
    "print(cindy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "95a37701",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'age': 16, 'height': 1.68, 'weight': 48, 'gender': 'female', 'name': 'cindy'}\n"
     ]
    }
   ],
   "source": [
    "coco.update(cindy)\n",
    "print(coco)\n",
    "# 如果合并的字典中包含已存在的key，会被覆盖"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "c11d64be",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'cindy', 'age': 16, 'height': 1.68, 'weight': 48, 'job': 'student', 'gender': 'female'}\n"
     ]
    }
   ],
   "source": [
    "new_information = {\"job\":\"student\",\n",
    "                   \"gender\":\"female\"}\n",
    "cindy.update(new_information)\n",
    "print(cindy)\n",
    "# 不包含则直接合并"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70d19477",
   "metadata": {},
   "source": [
    "### 清空"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "199ce2d0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n"
     ]
    }
   ],
   "source": [
    "cindy.clear()\n",
    "print(cindy)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b4f95005",
   "metadata": {},
   "source": [
    "### 循环遍历"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "3a9449cc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name-lily\n",
      "age-22\n"
     ]
    }
   ],
   "source": [
    "lily = {\"name\":\"lily\",\n",
    "        \"age\":22}\n",
    "for i in lily:\n",
    "    print(\"%s-%s\"%(i,lily[i]))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b3541784",
   "metadata": {},
   "source": [
    "## 字典查询"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "98f40653",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12\n"
     ]
    }
   ],
   "source": [
    "nana = {\"name\":\"nana\",\n",
    "        \"age\":12,\n",
    "        \"height\":1.60,\n",
    "        \"weight\":45}\n",
    "print(nana[\"age\"])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "176c3b2f",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'gender'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[0;32m/var/folders/c2/_wn0sq3s66v1nhs_127pw_sm0000gn/T/ipykernel_674/3967925233.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnana\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"gender\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m: 'gender'"
     ]
    }
   ],
   "source": [
    "print(nana[\"gender\"])\n",
    "# 指定的key不存在会报错！！！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "323d12fa",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
